package main

import (
	"fmt"
	"time"
)

var arrRequestFloor []int

type Elevator struct {
	intMaxFloor     int    //电梯最大楼层
	intMinFloor     int    //电梯最小楼层
	intCurrentFloor int    //当前楼层
	intSpeed        int    //电梯每层的运行时间
	intOpenDoorTime int    //电梯每次的开门时间
	strDirection    string //电梯运行方向 up =>上 down=>下 Don't move =>原地不动
	arrDownFloor    []int  //电梯下楼的队列表
	arrUpFloor      []int  //电梯上楼的队列表
}

//获取电梯运行的方向 当请求的第一个元素大于当前楼层时 电梯要上升 请求的第一个元素小于当前楼层时 电梯要下降
//请求的第一个元素等于当前楼层时 电梯也不动
func (lift *Elevator) getDirection(arrRequestFloor []int) string {
	if len(arrRequestFloor) == 0 || arrRequestFloor[0] == 0 || arrRequestFloor[0] == lift.intCurrentFloor {
		lift.strDirection = "Don't move"
		return lift.strDirection
	}
	if arrRequestFloor[0] > lift.intCurrentFloor {
		lift.strDirection = "up"
		return lift.strDirection
	}
	if arrRequestFloor[0] < lift.intCurrentFloor {
		lift.strDirection = "down"
		return lift.strDirection
	}
	return ""
}

//电梯移动的方法
func (lift *Elevator) move() {
	for _, intRequestFloor := range arrRequestFloor {
		//当前楼层大于目标楼层 则进入到下降队列
		if intRequestFloor < lift.intCurrentFloor {
			lift.arrDownFloor = append(lift.arrDownFloor, intRequestFloor)
		} else { //当前楼层小于等于目标楼层时 进入到上升队列里
			lift.arrUpFloor = append(lift.arrUpFloor, intRequestFloor)
		}
	}
	for {
		if len(arrRequestFloor) == 0 {
			break
		}
		lift.getDirection(arrRequestFloor)
		if lift.strDirection == "up" {
			fmt.Println("上升")
			lift.up(lift.intCurrentFloor)
		}
		if lift.strDirection == "down" {
			fmt.Println("下降")
			lift.down(lift.intCurrentFloor)
		}
	}
	fmt.Println("电梯暂停")
}

//电梯上升
func (lift *Elevator) up(intCurrentFloor int) {
	fmt.Println("电梯开始上升")
	for _, requestFloor := range lift.arrUpFloor {
		for i := lift.intCurrentFloor; i < requestFloor; i++ {
			lift.runTime()
			intCurrentFloor++
			fmt.Printf("电梯上升到了%d层\n", intCurrentFloor)
			if intCurrentFloor == requestFloor {
				lift.openDoor()
				lift.openDoorTime()
				lift.closeDoor()
				lift.intCurrentFloor = intCurrentFloor
				removeFloor(intCurrentFloor)
			}
			if intCurrentFloor == lift.intMaxFloor {
				return
			}
		}
	}
}

//电梯下降
func (lift *Elevator) down(intCurrentFloor int) {
	fmt.Println("电梯开始下降")
	for _, requestFloor := range lift.arrDownFloor {
		for i := lift.intCurrentFloor; i > requestFloor; i-- {
			lift.runTime()
			intCurrentFloor--
			fmt.Printf("电梯下降到了%d层\n", intCurrentFloor)
			if intCurrentFloor == requestFloor {
				lift.openDoor()
				lift.openDoorTime()
				lift.closeDoor()
				lift.intCurrentFloor = intCurrentFloor
				removeFloor(intCurrentFloor)
			}
			if intCurrentFloor == lift.intMaxFloor {
				return
			}
		}
	}
}

//删除请求楼层的指定元素 电梯每到一层 删除该值
func removeFloor(intFloor int) {
	for key, value := range arrRequestFloor {
		if value == intFloor {
			kk := key + 1
			arrRequestFloor = append(arrRequestFloor[:key], arrRequestFloor[kk:]...)
		}
	}
	return
}

//电梯开门的动作
func (lift *Elevator) openDoor() {
	fmt.Printf("电梯开门...\n")
}

//电梯关门的动作
func (lift *Elevator) closeDoor() {
	fmt.Printf("电梯关门...\n")
}

//电梯每次的运行时间
func (lift *Elevator) runTime() {
	time.Sleep(time.Duration(lift.intSpeed) * (time.Second))
}

//电梯每次的开门时间
func (lift *Elevator) openDoorTime() {
	time.Sleep(time.Duration(lift.intOpenDoorTime) * (time.Second))
}
